﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using STX.Admin.Models.BizFinancial;
using STX.Domain;
using STX.Service;

namespace STX.Admin.Controllers
{
    public class BizFinancialController : BasePages1Controller<BizActivity, IndexModel, AddModel, EditModel, QueryModel>
    {
        public BizAreaService BizAreaService { get; set; }
        public BizActivityService BizActivityService { get; set; }
        public BizMaterialCategoryService BizMaterialCategoryService { get; set; }
        public BizQuotePriceTemplateService BizQuotePriceTemplateService { get; set; }
        public BizQuotePriceService BizQuotePriceService { get; set; }
        public BizProjectService BizProjectService { get; set; }
        public BizSupplierService BizSupplierService { get; set; }
        public TabAdministratorService TabAdministratorService { get; set; }
        public BizActivityItemService BizActivityItemService { get; set; }

        #region 初始化

        protected override void OnInit(out Func<BaseService<BizActivity>> thisService,
            out Func<IndexModel> getIndexModelFunc, out Func<AddModel> getAddModelFunc,
            out Func<int, EditModel> getEditModelFunc)
        {
            thisService = () => BizActivityService;
            getIndexModelFunc = () => new IndexModel();
            getAddModelFunc = () => new AddModel();
            getEditModelFunc = id =>
            {
                var model = BizActivityService.FindModelById(id);
                if (model == null)
                {
                    throw new Exception("没有找到该条数据");
                }
                return new EditModel
                {
                    Id=id,
                    Name = model.Name,
                    ApplyInvoiceDate = model.ApplyInvoiceDate,
                    ApplyInvoiceMan = model.ApplyInvoiceMan,
                    IncreaseOrDecrease = model.IncreaseOrDecrease,
                    InvoiceNo = model.InvoiceNo,
                    MakeInvoiceDate = model.MakeInvoiceDate,
                    ReturnedAmount = model.ReturnedAmount,
                    ReturnedDate = model.ReturnedDate,
                    TotalQuotation = model.BizActivityItems.Sum(o=>o.QuotationPrice)
                };
            };
        }

        #endregion

        #region Methods 方法
        protected override long AddHandler(AddModel addModel)
        {
            throw new Exception("无权限");
        }

        protected override bool EditHandler(EditModel editModel)
        {
            var model = BizActivityService.FindModelById(editModel.Id);
            if (model == null)
            {
                throw new Exception("没有找到该条数据");
            }
            model.IncreaseOrDecrease = editModel.IncreaseOrDecrease;
            model.ApplyInvoiceDate = editModel.ApplyInvoiceDate;
            model.ApplyInvoiceMan = editModel.ApplyInvoiceMan;
            model.MakeInvoiceDate = editModel.MakeInvoiceDate;
            model.InvoiceNo = editModel.InvoiceNo;

            model.ReturnedDate = editModel.ReturnedDate;
            model.ReturnedAmount = editModel.ReturnedAmount;
            return  BizActivityService.Update(model);
        }

        protected override IQueryable<BizActivity> QueryHandler(QueryModel queryModel, IQueryable<BizActivity> queryable)
        {
            if (AdminContext.CurrentUser.Position != PositionEnum.SystemPower
                && AdminContext.CurrentUser.Position != PositionEnum.CFO
                && AdminContext.CurrentUser.Position != PositionEnum.FinancialManager)
            {
                throw new Exception("无权限操作此页面");
            }
            queryable = queryable.Where(o => o.Step == StepEnum.总经理审核成本 
                && o.Auditing == AuditingEnum.审核成功);
            if (!queryModel.Name.IsNullOrEmpty())
                queryable = queryable.Where(o => o.Name.Contains(queryModel.Name));
            return queryable;
        }
        #endregion

        public ActionResult PrintMonth()
        {
            return View();
        }

        public ActionResult PrintYear()
        {
            return View();
        }
    }
}
